'use strict';
const Handlebars = require('handlebars');
var fs = require('fs-extra');

var artifact;
if (process.argv[2]) {
    artifact = process.argv[2].trim();
} else {
    // when not in alfred environment.For unit test only.
    artifact = "tenmalife-platform";
}

var isWebsite = artifact.indexOf("website") >= 0;

// var globalConfigJson = fs.readJsonSync("/Users/Steve/tools/myGit/xNode/global-config.json", {throws: false});

const data = {
    "isWebsite": isWebsite,
    "artifact": artifact,
    "tomcatRootFilePath": "/Users/Steve/tools/myGit/common-tomcat-server/apache-tomcat-8.0.36/conf/Catalina/localhost/ROOT.xml",
    "mvnBash": "/Users/Steve/tools/apache-maven-3.3.9/bin/mvn",
    "gitHome": "/Users/Steve/tools/myGit"

};

//This is for nodeJs
const globalConfigJsonTmpl = `{
  "artifactId": "{{artifact}}",
  "gitHome": "{{gitHome}}",
  "mvnBash": "{{mvnBash}}",
  "tomcatRootFilePath2": "/Users/Steve/tools/myGit/common-tomcat-server/jdk7-apache-tomcat-8.0.36/conf/Catalina/localhost/ROOT.xml",
  "tomcatRootFilePath": "{{tomcatRootFilePath}}"
}
`;

//This is for java.
const globalConfigPropertiesTmpl = `artifactId = {{artifact}}
gitHome = {{gitHome}}
mvnBash = {{mvnBash}}
`;


//This is for
const tomcatRootFileTmpl = `{{#isWebsite}}<Context path=""
	docBase="/Users/Steve/tools/myGit/{{artifact}}/src/main/webapp"
	privileged="true" reloadable="false">
	<Resources className="org.apache.catalina.webresources.StandardRoot">
		<PreResources
			base="/Users/Steve/tools/myGit/industry-platform/target/dependency"
			className="org.apache.catalina.webresources.DirResourceSet"
			internalPath="/" webAppMount="/WEB-INF/lib" readOnly="true" />
	</Resources>
	<JarScanner scanAllDirectories="false" scanAllFiles="false"
		scanBootstrapClassPath="false" scanClassPath="false" />
	<Loader loaderClass="org.apache.catalina.loader.WebappClassLoader" />
</Context>
{{/isWebsite}}{{^isWebsite}}<Context path=""
	docBase="/Users/Steve/tools/myGit/{{artifact}}/src/main/webapp"
	privileged="true" reloadable="false">
	<Resources className="org.apache.catalina.webresources.StandardRoot">
		<PreResources base="/Users/Steve/tools/myGit/{{artifact}}/target/classes"
			className="org.apache.catalina.webresources.DirResourceSet"
			internalPath="/" webAppMount="/WEB-INF/classes" readOnly="false" />
		<PreResources
			base="/Users/Steve/tools/myGit/{{artifact}}/target/dependency"
			className="org.apache.catalina.webresources.DirResourceSet"
			internalPath="/" webAppMount="/WEB-INF/lib" readOnly="true" />
	</Resources>
	<JarScanner scanAllDirectories="false" scanAllFiles="false"
		scanBootstrapClassPath="false" scanClassPath="false" />
	<Loader loaderClass="org.apache.catalina.loader.WebappClassLoader" />
</Context>
{{/isWebsite}}
`;

const globalConfigJsonEngine = Handlebars.compile(globalConfigJsonTmpl);
const tomcatRootFileEngine = Handlebars.compile(tomcatRootFileTmpl);
const globalConfigPropertiesEngine = Handlebars.compile(globalConfigPropertiesTmpl);


const globalConfigJsonData = globalConfigJsonEngine(data);
const tomcatRootFileData = tomcatRootFileEngine(data);
const globalConfigPropertiesData = globalConfigPropertiesEngine(data);
// console.log(`result1 is:${result1}`);
// console.log(`result2 is:${result2}`);
// console.log(`result3 is:${result3}`);

fs.writeFile("/Users/Steve/tools/myGit/jUtils/src/main/resources/global-config.properties", globalConfigPropertiesData);
fs.writeFile("/Users/Steve/tools/myGit/xNode/global-config.json", globalConfigJsonData);
fs.writeFile(data.tomcatRootFilePath, tomcatRootFileData);

console.log("All success.");